www.gusucode.com > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM源码程序 > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM\stprtool\svm\psvm.m

    function psvm(data,labels,Alpha,bias,ker,arg,options )
% PSVM vizualizes decision function of binary SVM problem.
% psvm(data,labels,Alpha,bias,ker,arg,options )
%
% PSVM vizualizes decision function for binary SVM problem 
%  in 2D space. 
%
% Mandatory inputs:
%  data [2xM] contains M training 2-dimensional patterns.
%  labels [1xM] labels of training patterns (1 or 2).
%  Alpha [1xM] Lagrange multipliers of the traning patterns.
%  bias [1x1] bias of decision function.
%  ker [string] identifies kernel (see 'help kernel').
%  arg [...] arguments of given kernel (see 'help kernel').
%
% Optional inputs:
%  options.background [int] (default 0) if 1then backgroud 
%     is colored according to the value of decision function.
%  options.sv [int] (default 1) if 1 then the Support Vectors 
%     are marked.
%  options.margin [int] (default 1) if 1 then margin is displayed.
%  options.patterns [int] (default 1) if 1 then patterns are 
%     displayed.
%  options.gridx [int] (default 25) density of grid in y-axis.
%  options.gridy [int] (default 25) density of grid in y-axis.
%  options.psize [int] (default 5) size of patterns (points).
%  options.color [int] (default 'k') color of separating function.
%
% See also PMSVM, SVM.
%

% Statistical Pattern Recognition Toolbox, Vojtech Franc, Vaclav Hlavac
% (c) Czech Technical University Prague, http://cmp.felk.cvut.cz
% Written Vojtech Franc (diploma thesis) 23.12.1999, 5.4.2000
%
% Modifications
% 21-oct-2001, V.Franc
% 16-april-2001, V. Franc, created

% at least 6 arguments must enter the function
if nargin < 6,
   error('Not enough input arguments.');
   return;
end

% set up default options 
if nargin < 7,
   options.background = 0;
   options.sv = 1;
   options.margin = 1;
   options.patterns = 1;
   options.gridx = 25;
   options.gridy = 25;   
   options.psize = 5;
   options.color = 'k';
else
   if ~isfield(options,'background'), options.background = 0; end
   if ~isfield(options,'sv'), options.sv = 1; end
   if ~isfield(options,'margin'), options.margin = 1; end
   if ~isfield(options,'patterns'), options.patterns = 1; end
   if ~isfield(options,'gridx'), options.gridx = 25; end
   if ~isfield(options,'gridy'), options.gridy = 25; end
   if ~isfield(options,'psize'), options.psize = 5; end
   if ~isfield(options,'color'), options.color = 'k'; end
end

% Lagrangians greater then ZERO_LIMIT are treated as Support Vectors
ZERO_LIMIT =1e-9;  

% plots Support Vectors
if options.patterns,
   ppatterns(data,labels,options.psize);
end

% get axis
a = axis;
old_hold = ishold;
hold on;

% plot Support Vectos
if options.sv,
   inx1 = find( Alpha > ZERO_LIMIT & labels == 1);
   inx1 = [inx1,find( Alpha < -ZERO_LIMIT & labels == 1)];
   if ~isempty(inx1),
      ppatterns(data(:,inx1), ['o' color(1)], options.psize+4 );
   end
   inx2 = find( Alpha > ZERO_LIMIT & labels == 2);
   inx2 = [inx2,find( Alpha < -ZERO_LIMIT & labels == 2)];
   if ~isempty(inx2),
      ppatterns(data(:,inx2), ['o' color(2)], options.psize+4 );
   end
end

% limits of current figure
xmin=a(1);
xmax=a(2);
ymin=a(3);
ymax=a(4);
  
% makes grid 
[X,Y] = meshgrid(xmin:(xmax-xmin)/options.gridx:xmax,...
                 ymin:(ymax-ymin)/options.gridy:ymax);

% make testing patterns covering whole grid
tst_data=[reshape(X',1,prod(size(X)));reshape(Y',1,prod(size(Y)))];

% classify points
[pred_labels, dec_fun] = svmclass2(tst_data,data,labels,Alpha,bias,ker,arg);

% compute color limits
l=(-min(dec_fun)+max(dec_fun))/2;

% reshape dec_fun
Z = reshape(dec_fun,size(X,1),size(X,2))';

% colors background 
if options.background,
  phandle = pcolor(X,Y,Z);
end

% smooth shading
shading interp;

% plots decision boundary
contour(X,Y,Z,[0,0],options.color);

% plots margins
if options.margin,
   contour(X,Y,Z,[1,1],[options.color,'--']);
   contour(X,Y,Z,[-1,-1],[options.color,'--']);
end

% set color limits and colormap
if options.background,
  set(phandle, 'LineStyle','none' );
  set(gca,'Clim',[-l l]);
  
  % creates colormap and sets it up
  g=gray(64);
  cmp=[g(33:end,:);flipud(g(33:end,:))];
  cmp(1:32,1)=cmp(1:32,1)/2;
  cmp(1:32,3)=cmp(1:32,3)/2;
  cmp(33:end,3)=cmp(33:end,3)/2;
%  cmp(33:end,2)=cmp(33:end,2)/2;
  colormap(cmp)
end

if ~old_hold,
   hold off;
end

return;